﻿;( function()
{
    var Upload = Upload || {};

    Upload.config = {};

    Upload.init = function( obj )
    {
        this._uploadFile = null;
        this.startDate = null;
        this.appPath = obj.appPath || null;
        this.readFileLoadError = obj.readFileLoadError || function(){};
        this.readFileLoadStart = obj.readFileLoadStart || function(){};
        this.readFileSuccess = obj.readFileSuccess || function(){};
        this.readFileProgress = obj.readFileProgress || function( loaded, total, e, file ){};
        this.onProgress = obj.onProgress || function( file, loaded, total, startDate ){};
        this.onUploadComplete = obj.onUploadComplete || function(){};
        this.onUploadError = obj.onUploadError || function( file, responseText ){};
        this.onUploadStart = obj.onUploadStart || function(){};

        this._bool = true;
    };

    Upload.init.prototype = {

        /** 读取文件信息*/
        readFile : function( uploadfile )
        {
            var _this = this;

            _this._uploadFile = uploadfile || null;

            if( !_this._uploadFile ) return false;

            var oFileReader = new FileReader();

            oFileReader.readAsDataURL( _this._uploadFile );
            // 读取错误时
            oFileReader.oneror = _this.readFileLoadError;
            // 开始读取数据
            oFileReader.onloadstart = _this.readFileLoadStart;
            // 文件读取完成时
            oFileReader.onload = function( res )
            {
                _this.readFileSuccess.call( _this, res, _this._uploadFile );
            }

            oFileReader.onprogress = function( e )
            {
                _this.readFileProgress.call( _this, e.loaded, e.total, e, _this._uploadFile );
            }
        },

        /** 上传数据 */
        uploadData : function( uploadfile, data )
        {
            if( !( this.appPath && uploadfile && this._bool ) ) return false;
			
            this._bool = false;

            var xhr = new XMLHttpRequest();

            if ( xhr.upload )
            {
                var self = this;
                var path = this.appPath;
                var file = uploadfile;

                // 上传中
                xhr.upload.addEventListener("progress", function( e )
                {
                    self.onProgress.call( self, file, e.loaded, e.total, self.startDate );

                }, false );

                // 文件上传成功或是失败
                xhr.onreadystatechange = function()
                {
                    if ( xhr.readyState == 4 )
                    {
                        if ( xhr.status == 200 )
                        {
							self._bool = true;
							
                            self.onUploadComplete.call( self, file, xhr.responseText );
                        }
                        else
                        {
                            self.onUploadError.call( self, xhr.responseText);
                        }
                    }
                };

                self.onUploadStart.call( self );

                // 开始上传
                xhr.open( "POST", path, true );

                xhr.setRequestHeader("X_FILENAME", encodeURIComponent( file.name ) );

                for( var attr in data )
                {
                    xhr.setRequestHeader( attr , encodeURIComponent( data[ attr ] ) );
                }

                xhr.send( file );
            }

        },

        /** 将文件的单位由bytes转换为KB或MB */
        formatFileSize : function( size, type )
        {
            var result = [];

            if( type == "mb" )
            {
                result[ 0 ] = parseFloat( Math.round(size * 100 / (1024 * 1024)) / 100 );
                result[ 1 ] = 'MB';
            }
            else
            {
                result[ 0 ] = parseFloat( Math.round(size * 100 / 1024) / 100 );
                result[ 1 ] = 'KB';
            }

            return result;
        }
    };

    window.Upload = Upload.init;
})();
